package uk.ac.cam.cl.oscar.sift;

import java.awt.image.BufferedImage;

/**
 * Wrapper class for image manipulation functions.
 */
public class ImageUtil
{
    public static BufferedImage ScaleHalf(BufferedImage image)
    {
        BufferedImage result = new BufferedImage(image.getWidth() / 2, image.getHeight() / 2, BufferedImage.TYPE_INT_ARGB);

        for (int y=0; y<result.getHeight(); y++) {
            for (int x=0; x<result.getWidth(); x++) {
                result.setRGB(x, y, image.getRGB(2*x, 2*y));
            }
        }
        return result;
    }

    public static BufferedImage ScaleDouble(BufferedImage image)
    {
        BufferedImage result = new BufferedImage(image.getWidth() * 2, image.getHeight() * 2, BufferedImage.TYPE_INT_ARGB);

        for (int y=0; y<(image.getHeight()-1); y++) {
            for (int x=0; x<(image.getWidth()-1); x++) {
                result.setRGB(2*x, 2*y, image.getRGB(x,y));
                result.setRGB(2*x+1, 2*y, (image.getRGB(x,y) + image.getRGB(x+1,y)) / 2);
                result.setRGB(2*x, 2*y+1, (image.getRGB(x,y) + image.getRGB(x,y+1)) / 2);
                result.setRGB(2*x+1, 2*y+1, (image.getRGB(x,y) + image.getRGB(x+1, y) +
                                             image.getRGB(x,y+1) + image.getRGB(x+1,y+1)) / 4);
            }
        }
        return result;
    }
    
    /** Converts an integer encoded 8x3 bit RGB value to a scaler 0-255 */
	public static int RGBtoMagnitude (int rgb) 
	{ 
		return ((rgb & 0xFF) + (rgb>>8 & 0xFF) + (rgb>>16 & 0xFF)) / 3;
	}
}
